package com.sx.baseproject.module

import android.view.KeyEvent
import android.view.MenuItem
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.BaseFragment
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.ActivityManager
import com.example.mvvm.until.Tips
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.sx.baseproject.BR
import com.sx.baseproject.R
import com.sx.baseproject.databinding.ActMainBinding
import com.sx.baseproject.framework.app.MyActivity
import com.sx.baseproject.framework.arouth.RouteManager
import com.sx.baseproject.framework.arouth.RoutePath
import com.sx.baseproject.framework.model.MainModel
import java.util.*

@Route(path = RoutePath.MAIN)
class MainActivity : MyActivity<ActMainBinding>(),
    BottomNavigationView.OnNavigationItemSelectedListener {

    private val baseFragments: List<BaseFragment<*, MainActivity>> by lazy {
        listOf(
            RouteManager.getInstance().getGroupOrderFragment(),
            RouteManager.getInstance().getLiveFragment(),
            RouteManager.getInstance().getMineFragment()
        )
    }


    private val viewModel: MainModel by lazy {
        getActivityScopeViewModel(MainModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig =
        DataBindingConfig(R.layout.act_main, BR.viewModel, viewModel)

    override fun initViewModel() {
        super.initViewModel()
    }

    override fun initView() {
        binding.mainNavigation.itemIconTintList = null
        binding.mainNavigation.setOnNavigationItemSelectedListener(this)
        onFragmentChangeSelected(0)
    }

    override fun initData() {
        viewModel.getBean()
        viewModel.getList()
    }

    /**
     * 连续按两次返回键就退出
     */
    private var keyBackClickCount = 0
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            when (keyBackClickCount++) {
                0 -> {
                    Tips.show(this, "再点一次退出")
                    val timer = Timer()
                    timer.schedule(object : TimerTask() {
                        override fun run() {
                            keyBackClickCount = 0
                        }
                    }, 3000)
                }
                1 -> {
                    ActivityManager.getInstance().exit()
                    finish()
                }
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu1 -> {
                onFragmentChangeSelected(0)
            }
            R.id.menu2 -> {
                onFragmentChangeSelected(1)
            }
            R.id.menu3 -> {
                onFragmentChangeSelected(2)
            }
        }
        return true
    }

    private var baseFragment: BaseFragment<*, MainActivity>? = null

    fun onFragmentChangeSelected(position: Int) {
        val fragmentManager = this.supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        for (i in baseFragments.indices) {
            val tempFragment: BaseFragment<*, MainActivity> = baseFragments.get(i)
            if (position == i) {
                if (tempFragment.isAdded) {
                    if (baseFragment == null) {
                        baseFragment = tempFragment
                    }
                    transaction.hide(baseFragment!!).show(tempFragment).commitAllowingStateLoss()
                } else {
                    if (baseFragment == null) {
                        baseFragment = tempFragment
                        transaction.add(R.id.nav_host_fragment, baseFragment!!)
                            .commitAllowingStateLoss()
                    } else {
                        transaction.hide(baseFragment!!).add(R.id.nav_host_fragment, tempFragment)
                            .commitAllowingStateLoss()
                    }
                }
                baseFragment = tempFragment
                break
            }
        }
    }

}